define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/index' + location.search,
                    add_url: 'store/add',
                    edit_url: 'store/edit',
                    del_url: 'store/del',
                    multi_url: 'store/multi',
                    import_url: 'store/import',
                    table: 'store',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'store_name', title: __('Store_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'real_address', title: __('Real_address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'zz_num', title: __('Zz_num'), operate: 'LIKE'},
                        {field: 'store_type', title: __('Store_type'), operate: 'LIKE'},
                        // {field: 'registered_price', title: __('Registered_price'), operate: 'LIKE'},
                        // {field: 'establishment_day', title: __('Establishment_day'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'tel', title: __('Tel'), operate: false},
                        {field: 'contact_name', title: __('Contact_name'), operate: 'LIKE'},
                        {field: 'contact_tel', title: __('Contact_tel'), operate: false},
                        // {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'registration_authority', title: __('Registration_authority'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'business_scope', title: __('Business_scope'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'car_count', title: __('Car_count')},
                        // {field: 'install_count', title: __('Install_count')},
                        // {field: 'road_no', title: __('Road_no'), operate: 'LIKE'},
                        // {field: 'road_endtime', title: __('Road_endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'add_time', title: __('Add_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'edit_time', title: __('Edit_time'), operate: 'LIKE'},
                        // {field: 'password', title: __('Password'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'is_jin', title: __('Is_jin'), searchList: {"0":__('Is_jin 0'),"1":__('Is_jin 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'is_del', title: __('Is_del'), searchList: {"0":__('Is_del 0'),"1":__('Is_del 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        // {field: 'authen_time', title: __('Authen_time'), operate: 'LIKE'},
                        // {field: 'open_id', title: __('Open_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                            {
                                name: 'fenpei',
                                title: '分配后台账号',
                                //extend:'data-area=["860px","730px"]',
                                text:'分配后台账号',
                              
                                hidden: function(row){
                                    if (row.admin.username == null) {
                                        return false;
                                    }
                                    return true;
                                },
                                
                                //height:'500px',
                                classname: 'btn btn-xs btn-info btn-primary btn-dialog',
                                url: 'auth/admin/add?store_id={id}',
                                callback: function (data) {
                                    //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                }
                            },
                            {
                                name: 'eidtaccount',
                                title: '修改后台账号',
                                
                                //extend:'data-area=["860px","730px"]',
                                text:'修改后台账号',
                                hidden: function(row){
                                    if (row.admin.username == null) {
                                        return true;
                                    }
                                    return false;
                                },
                                //height:'500px',
                                classname: 'btn btn-xs btn-success btn-primary btn-dialog',
                                url: 'auth/admin/edit?store_id={id}',
                                callback: function (data) {
                                    //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                }
                            },
                            {
                                name: 'changelog',
                                title: '变动记录',
                                extend:'data-area=["1000px","730px"]',
                                text:'变动记录',
                                //height:'500px',
                                classname: 'btn btn-xs btn-waring btn-primary btn-dialog',
                                url: 'changelog?change_id={id}&type=1',
                                callback: function (data) {
                                    //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                }
                            },
                            
                        ]}
                    ]
                ]
            });
            // 为表格绑定事件
            //*************************** 自定义export开始
            var submitForm = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
            };
            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                console.log(ids, page, all);
                Layer.confirm("<form action='" + Fast.api.fixurl("store/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                    title: '导出数据',
                    // btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    btn: ["全部(" + all + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn3: function (index, layero) {
                        submitForm("all", layero);
                        return false;
                    }
                })
            });
            //*************************** 自定义export结束
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-status", function(){
                var authen=$("#c-status").val();
                // alert(authen);
                if(authen=='4')
                {
                    $("#remark").show();
                    $(".tong").hide();
                }
                else
                {
                    $("#remark").hide();
                    $(".tong").show();
                }

            });
            //不验证不可见表单
            $('form[role=form]').data("validator-options", {
                ignore: ':hidden'
            });
            Controller.api.bindevent();
        },
        edit_self: function () {
            
            //不验证不可见表单
            $('form[role=form]').data("validator-options", {
                ignore: ':hidden'
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
